package maps;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

public class MapParser {

	private Map<Integer, GcAdder> colorToGcMap;
	
	
	public MapParser(BufferedImage colorsCfg, Map<Integer, GcAdder> addersMap){
		this.initColorMap(colorsCfg, addersMap);
	}


	private void initColorMap(BufferedImage colorsCfg,
			Map<Integer, GcAdder> addersMap) {

		int colors = addersMap.size();
		this.colorToGcMap = new HashMap<Integer, GcAdder>();
		for(int i= 0; i < colors; i++){
			int color = colorsCfg.getRGB(i, 0);
			GcAdder adder = addersMap.get(i);
			this.colorToGcMap.put(color, adder);
		}
	}
	
	public void parseImage(BufferedImage map, Addeable scene){
		int width, height, color;
		GcAdder adder;
		
		width = map.getWidth();
		height = map.getHeight();
		
		for (int i = 0; i < width; i++){
			for (int ii = 0; ii < height; ii++){
				color = map.getRGB(i, ii);
				adder = this.colorToGcMap.get(color);
				adder.addToScene(scene, i, ii);
			}
		}
	}
	public static BufferedImage getImage(String path){
		BufferedImage image = null;
		try {
			image = ImageIO.read(MapParser.class.getResource(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return image;
	}
}
